#include "recbox.h"
#include "ui_recbox.h"
#include"recbox.h"
#include"recboxitem.h"
#include<QJsonParseError>

RecBox::RecBox(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecBox),
    row(1),
    col(4)
{
    ui->setupUi(this);

//    RecBoxItem*item=new RecBoxItem();
//    ui->recListDownHLayout->addWidget(item);



}

RecBox::~RecBox()
{
    delete ui;
}

void RecBox::initRecBoxUi(QJsonArray data, int row)
{
    Q_UNUSED(data);// 不再使用传入数据，使用网络数据

    if(row==2){
        this->row=row;
        col=8;
    }
    else{
        ui->recListDown->hide();
    }

    if (!manager) {
            manager = new QNetworkAccessManager(this);
    }

    QUrl url("http://127.0.0.1:8080/fullMusicList");
    QNetworkRequest request(url);

//    imageList=data;

    QNetworkReply *reply = manager->get(request);
       connect(reply, &QNetworkReply::finished, this, [=]() {
           if (reply->error() != QNetworkReply::NoError) {
               qDebug() << "网络请求失败:" << reply->errorString();
               reply->deleteLater();
               return;
           }

           QByteArray resp = reply->readAll();
           reply->deleteLater();

           // 解析 JSON
           QJsonParseError err;
           QJsonDocument doc = QJsonDocument::fromJson(resp, &err);
           if (err.error != QJsonParseError::NoError || !doc.isArray()) {
               qDebug() << "解析 JSON 失败:" << err.errorString();
               return;
           }

           imageList = doc.array();              // 保存数据


           currentIndex = 0;
           count = imageList.size() / col;       // 计算页数
           createRecBoxItem();                   // 更新界面
       });


}

void RecBox::createRecBoxItem()
{
    QList<RecBoxItem*>recUpList = ui->recListUp->findChildren<RecBoxItem*>();

    for(auto i:recUpList){
        ui->recListUpHLayout->removeWidget(i);
        delete i;
    }

    QList<RecBoxItem*>recDownList = ui->recListDown->findChildren<RecBoxItem*>();

    for(auto i:recDownList){
        ui->recListDownHLayout->removeWidget(i);
        delete i;
    }

    //创建RecBoxItem对象，往RecBox添加

    int index=0;
    for(int i=currentIndex*col;i<currentIndex*col+col;i++){

        QString name = imageList[i].toString();
        RecBoxItem *item=new RecBoxItem();

        connect(item,&RecBoxItem::netMusicListready,this,&RecBox::onMusicListItemReady);//链接歌单按钮生成net界面

        // 设置文字
        item->setRecText(name);

        // 拼封面 URL
        QString coverUrl = QString("http://127.0.0.1:8080/cover/%1.jpg").arg(name);

        qDebug()<<"封面的url为:"<<coverUrl;
        item->setRecImage(coverUrl);

//        QJsonObject obj=imageList[i].toObject();
//        item->setRecText(obj.value("text").toString());
//        item->setRecImage(obj.value("path").toString());

        if(index>=col/2&&row==2){
            ui->recListDownHLayout->addWidget(item);
        }

        else{
            ui->recListUpHLayout->addWidget(item);
        }
        index++;
    }
}

void RecBox::onMusicListItemReady(const MusicList &netList, const QString &listName)
{
     emit netMusicListready(netList, listName);
}

void RecBox::on_btDown_clicked()
{
    currentIndex++;
    if(currentIndex>=count)currentIndex=0;

    createRecBoxItem();
}

void RecBox::on_btUp_clicked()
{
    currentIndex--;
    if(currentIndex<0)currentIndex=count-1;

    createRecBoxItem();
}
